<template>
    <view :class="'zan-actionsheet ' + (show ? 'zan-actionsheet--show' : '')">
        <view class="mask-class zan-actionsheet__mask" @tap="onMaskClick"></view>
        <view class="container-class zan-actionsheet__container">
            <!-- 选项按钮 -->
            <zan-btn
                @btnclick="handleBtnClick(index)"
                :open-type="item.openType"
                custom-class="zan-actionsheet__btn"
                :loading="item.loading"
                v-for="(item, index) in actions"
                :key="index"
            >
                <!-- 自定义组件控制 slot 样式有问题，故在 slot 容器上传入 loading 信息 -->

                <view :class="'zan-actionsheet__btn-content ' + (item.loading ? 'zan-actionsheet__btn--loading' : '')">
                    <view class="zan-actionsheet__name">{{ item.name }}</view>
                    <view v-if="item.subname" class="zan-actionsheet__subname">
                        {{ item.subname }}
                    </view>
                </view>
            </zan-btn>

            <!-- 关闭按钮 -->
            <view v-if="cancelText" class="zan-actionsheet__footer">
                <zan-btn custom-class="zan-actionsheet__btn" @btnclick="cancelClick">{{ cancelText }}</zan-btn>
            </view>
        </view>
    </view>
</template>

<script>
'use strict';
import zanBtn from '../btn/index';

export default {
    data() {
        return {};
    },
    components: {
        zanBtn
    },
    externalClasses: ['mask-class', 'container-class'],
    props: {
        actions: {
            type: Array,
            default: () => []
        },
        show: {
            type: Boolean,
            default: false
        },
        cancelWithMask: {
            type: Boolean,
            default: true
        },
        cancelText: {
            type: String,
            default: ''
        }
    },
    methods: {
        onMaskClick: function onMaskClick() {
            if (this.cancelWithMask) {
                this.cancelClick();
            }
        },
        cancelClick: function cancelClick() {
            this.$emit('cancel');
        },
        handleBtnClick: function handleBtnClick(index) {
            this.$emit('actionclick', {
                detail: {
                    index: index
                }
            });
        }
    }
};
</script>
<style lang="scss" scoped>
.zan-actionsheet {
    background-color: $uni-bg-color-grey;
}

.zan-actionsheet__mask {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 10;
    background: rgba(0, 0, 0, 0.7);
    display: none;
}

.zan-actionsheet__container {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    background: $uni-bg-color-grey;
    -webkit-transform: translate3d(0, 50%, 0);
    transform: translate3d(0, 50%, 0);
    -webkit-transform-origin: center;
    transform-origin: center;
    -webkit-transition: all 0.2s ease;
    transition: all 0.2s ease;
    z-index: 11;
    opacity: 0;
    visibility: hidden;
	padding-bottom: constant(safe-area-inset-bottom);
    padding-bottom: env(safe-area-inset-bottom);
}

.zan-actionsheet__btn {
    margin-bottom: 0 !important;
}

.zan-actionsheet__footer .zan-actionsheet__btn {
    background: #fff;
}

.zan-actionsheet__btn-content {
    display: -webkit-box;
    display: flex;
    -webkit-box-orient: horizontal;
    -webkit-box-direction: normal;
    flex-direction: row;
    -webkit-box-pack: center;
    justify-content: center;
}

.zan-actionsheet__subname {
    color: $uni-text-color-grey;
}

.zan-actionsheet__name,
.zan-actionsheet__subname {
    height: 45px;
    line-height: 45px;
}

.zan-actionsheet__btn.zan-btn:last-child::after {
    border-bottom-width: 0;
}

.zan-actionsheet__subname {
    margin-left: 2px;
    font-size: 12px;
}

.zan-actionsheet__footer {
    margin-top: 10px;
}

.zan-actionsheet__btn--loading .zan-actionsheet__subname {
    color: transparent;
}

.zan-actionsheet--show .zan-actionsheet__container {
    opacity: 1;
    -webkit-transform: translate3d(0, 0, 0);
    transform: translate3d(0, 0, 0);
    visibility: visible;
}

.zan-actionsheet--show .zan-actionsheet__mask {
    display: block;
}
</style>
